package org.dong.samples.dao.Impl;

import java.util.ArrayList;
import java.util.List;
import org.dong.samples.dao.UserHibernateDAO;
import org.dong.samples.model.PagerModel;
import org.dong.samples.model.SystemContext;
import org.dong.samples.model.User;
import org.dong.samples.model.vo.UserVO;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
//import org.springframework.transaction.annotation.Transactional;

@Repository
//@Transactional
public class UserHibernateDAOImpl extends BaseHibernateDAO<User> implements UserHibernateDAO {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
	
	@Override
	public void createUser(User user) throws Exception {
		if(user.getAddress()==""){
			user.setAddress("未填写");
		}
		if(user.getTel()==""){
			user.setTel("未填写");
		}
		user.setPassword("123456");
		user.setEmail(user.getUserId()+"@qq.com");
		Session session = getSession();
		session.save(user);
	}
	
	@Override
	public void editUser(User user) throws Exception {
		if(user.getAddress()==""){
			user.setAddress("未填写");
		}
		if(user.getTel()==""){
			user.setTel("未填写");
		}
		user.setPassword("123456");
		user.setEmail(user.getUserId()+"@qq.com");
		Session session = getSession();
		session.update(user);
	}
	
	@Override
	public void deleteUser(String id) throws Exception {
		Session session = getSession();
		User user = (User) session.get(User.class, id);
		if(user != null){
			session.delete(user);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllUser() throws Exception {
		Session session = getSession();
		String hql = "FROM User";
		List<User> user = session.createQuery(hql).list();
		return user;
	}
	
	@Override
	public User getUserById(String userId) throws Exception {
		Session session = getSession();
		return (User) session.get(User.class, userId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<UserVO> pageFindUser() throws Exception {
		Session session = getSession();
		PagerModel<UserVO> page = new PagerModel<UserVO>();
		List<UserVO> list = new ArrayList<UserVO>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getPageSize();
		//分页
		List<User> userList = session.createQuery("FROM User").setFirstResult(offset).setMaxResults(pageSize).list();
		for(User user:userList){
			UserVO vo = new UserVO();
			BeanUtils.copyProperties(user, vo);
			list.add(vo);
		}
		page.setDates(list);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		long totalSize = (long)this.getSession().createQuery("select count(*) from User").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}

	@Override
	public void insertTime(String time, String userId) {
		Session session = getSession();
		String hql = " update exam_user set last_lock_time='"+time+"' where user_id='"+userId+"' ";
		session.createSQLQuery(hql);
		
	}

	@Override
	public void insertCount(Integer count, String userId) {
//		Session session = getSession();
//		String hql = "update exam_user set login_fail_count="+count+" where user_id="+userId+" ";
//		String hqld = "update User set login_fail_count=? where user_id=?";
//		session.update(hqld, new Object[]{count,userId});
//		session.createSQLQuery(hql);
	}
	
}
